/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.entity.BidItemGroup;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  查询标的分组列表
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidItemGroupListPlugin implements PluginConnector {
	private IDao bidItemGroupDao;
	private Logger logger = Logger.getLogger(BidItemGroupListPlugin.class);

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();

		try {
			result.setResult("true");

			MethodParam param = setMethodParams(vo, 2, "");
			if (Validator.isEmpty(param)) {
				result.setResult("false");
				result.setMessage("查询条件为空!");
				return result;
			}
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			param.setResultCache(false);
			//param.setOrderby(orderBy);
			String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
					" order by open_time desc ");
			param.setParams("orderBy", orderBy);
			if (pageSize == 0 && pageIndex == 0) {
				List list = bidItemGroupDao.select(param);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				Page page = bidItemGroupDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			logger.error("执行BidItemGroupList查询失败 ", e);
			result.setMessage("BidItemGroupList查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		MethodParam param = new MethodParam("ByProperty", "", "", BidItemGroup.class.getName());
		param.setResultCache(false);
		String all = String.valueOf(vo.getParams("bidItemGroupAll"));//获取所有标的分组列表
		if (Validator.isNull(all)) {
			getBidItemGroupParams(param, vo);
			if (Validator.isEmpty(param.getParams()) || param.getParams().size() == 0)
				return null;
		}
		return param;
	}

	//组织BidItemGroup信息查询条件
	private void getBidItemGroupParams(MethodParam param, ParamsVo vo) {
		BidItemGroup bidItemGroup = new BidItemGroup();
		bidItemGroup = (BidItemGroup) WbdToolsUtil.getInstance().convert2Bean(vo.getParams(), bidItemGroup);
		if (Validator.isNotNull(bidItemGroup.getGroupCode())) {
			param.setParams("groupCode", bidItemGroup.getGroupCode());
		}
		if (Validator.isNotNull(bidItemGroup.getGroupName())) {
			param.setParams("groupName", " like '%" + bidItemGroup.getGroupName() + "%' ");
		}
		if (Validator.isNotNull(bidItemGroup.getSessionId())) {
			param.setParams("sessionId", bidItemGroup.getSessionId());
		}
		if (Validator.isNotNull(bidItemGroup.getCreateTime())) {
			param.setParams("createTime", bidItemGroup.getCreateTime());
		}
		if (Validator.isNotNull(bidItemGroup.getModifyTime())) {
			param.setParams("modifyTime", bidItemGroup.getModifyTime());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare1())) {
			param.setParams("spare1", bidItemGroup.getSpare1());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare2())) {
			param.setParams("spare2", bidItemGroup.getSpare2());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare3())) {
			param.setParams("spare3", bidItemGroup.getSpare3());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare4())) {
			param.setParams("spare4", bidItemGroup.getSpare4());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare5())) {
			param.setParams("spare5", bidItemGroup.getSpare5());
		}
	}

	public void setBidItemGroupDao(IDao bidItemGroupDao) {
		this.bidItemGroupDao = bidItemGroupDao;
	}

}
